import Koa from 'koa'
import cors from 'koa2-cors'
import bodyParser from 'koa-bodyparser'
import staticKoa from 'koa-static'
import {dbInstance} from './db/config'
import model from './model'
import { router } from './routes'
// 初始化 Koa 应用实例
const app = new Koa();

// 注册中间件
app.use(cors());
app.use(bodyParser());


app.use(router())

// 定义各键关联约束,不能注释
await model.defineRelation()

// 建表语句，确定建好后注释掉即可
// const res=await model.createTable()
// console.log("已经创建了表",res)

// 响应用户请求
app.use(async (ctx) => {
  try {
    await dbInstance.authenticate();
    console.log('Connection has been established successfully.');
  } catch (error) {
    console.error('Unable to connect to the database:', error); 
  }
  ctx.body = 'Hello Koa';
});

// 运行服务器
app.listen(3000,()=>{
  console.log("server is running at http://localhost:3000");
});
